package sy4_Creational_Pattern.builder.hlb;

import lombok.Data;

/**
 * 胡乐滨提供
 * 类Computer包含内部类Builder
 */

@Data   //使用本注解，多产生了一些方法，如canEqual()
public class Computor {
    private final String cpu; //必须
    private final String ram; //必须
    private final int usbCount; //可选
    private final String keyboard; //可选
    private final String display; //可选

    private Computor(Builder builder) {  //私有构造方法，供内部类Builder的 build()调用
        this.cpu = builder.cpu;
        this.ram = builder.ram;
        this.usbCount = builder.usbCount;
        this.keyboard = builder.keyboard;
        this.display = builder.display;
    }

    public static class Builder {  //内部类
        private String cpu;//必须
        private String ram;//必须
        private int usbCount;//可选
        private String keyboard;//可选
        private String display;//可选

        public Builder(String cup, String ram) {  //内部类的构造方法
            this.cpu = cup;
            this.ram = ram;
        }

        public Builder setUsbCount(int usbCount) {
            this.usbCount = usbCount;
            return this;  //支持流式写法的关键
        }

        public Builder setKeyboard(String keyboard) {
            this.keyboard = keyboard;
            return this;
        }

        public Builder setDisplay(String display) {
            this.display = display;
            return this;  //支持流式写法的关键
        }

        public Computor build() {  //返回产品
            //调用外部类的私有构造方法，并以当前内部类对象作为构造参数
            return new Computor(this);
        }
    }

    public static void main(String[] args) {
        /*Builder builder = new Builder("Intel CPU", "8GRam");
        builder.setDisplay("三星显示器")
                .setKeyboard("双飞燕键盘");
        Computer computer = builder.build();*/

        Computor computor = new Builder("Intel CPU", "8GRam")  //构建
                .setDisplay("三星显示器")  //构建
                .setKeyboard("双飞燕键盘")  //构建
                .setUsbCount(2)  //构建
                .build();  //建造成完整产品
        System.out.println(computor);
    }
}
